package org.jash.shop2201.entity

import androidx.databinding.BaseObservable
import androidx.databinding.Bindable

class Cart:BaseObservable() {
    val data = mutableListOf<CartItem>()
    @get:Bindable
    var all:Int
        get() = when(data.filter(CartItem::checked).size) {
            0 -> 0
            data.size -> 1
            else -> 2
        }
        set(value) {
            if(value != 2) {
                data.forEach { it.checked = value == 1 }
            }
        }
    @get:Bindable("all")
    val total:Float
        get() = data.filter(CartItem::checked).map {it.goods_default_price * it.count}.sum()
    @get:Bindable("all")
    val coupon:Float
        get() = data.filter(CartItem::checked).map(CartItem::coupon).sum()
}